package com.wigem.fa.service;

import java.util.List;

import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.fa.entity.FaEntInfoEntity;
import com.wigem.fa.mapper.FaEntInfoMapper;
import com.wigem.fa.vo.FaEntInfoVo;
import com.wigem.framework.sa.token.core.UserInfo;
import com.wigem.framework.sa.token.core.util.SaTokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 申报企业基本信息Service业务层处理
 *
 * @author DH
 * @date 2024-10-20
 */
@Service
public class FaEntInfoServiceImpl implements FaEntInfoService {

    @Resource
    private FaEntInfoMapper faEntInfoMapper;


    @Override
    public List<FaEntInfoEntity> getList(FaEntInfoVo faEntInfovo) {

        return faEntInfoMapper.selectList(query(faEntInfovo));
    }

    @Override
    public PageResult<FaEntInfoEntity> getPageList(FaEntInfoVo faEntInfoVo) {

        return faEntInfoMapper.selectPage(faEntInfoVo, query(faEntInfoVo));
    }


    @Override
    public FaEntInfoEntity getEntity(String id) {
        return faEntInfoMapper.selectById(id);
    }

    public FaEntInfoEntity getEntityByCreateUser() {
        UserInfo loginUserInfo = SaTokenUtils.getLoginUserInfo();
        return faEntInfoMapper.getEntityByCreateUser(loginUserInfo.getUserId());
    }

    @Override
    public void add(FaEntInfoVo faEntInfoVo) {
        FaEntInfoEntity faEntInfoEntity = new FaEntInfoEntity();
        BeanUtils.copyProperties(faEntInfoVo, faEntInfoEntity);
        faEntInfoEntity.create();
        faEntInfoMapper.insert(faEntInfoEntity);
    }


    @Override
    public void update(FaEntInfoVo faEntInfoVo) {
        FaEntInfoEntity faEntInfoEntity = new FaEntInfoEntity();
        BeanUtils.copyProperties(faEntInfoVo, faEntInfoEntity);
        faEntInfoMapper.updateById(faEntInfoEntity);
    }

    @Override
    public void save(FaEntInfoVo faEntInfoVo) {
        FaEntInfoEntity faEntInfoEntity = new FaEntInfoEntity();
        BeanUtils.copyProperties(faEntInfoVo, faEntInfoEntity);
        if (StringUtils.isEmpty(faEntInfoVo.getId())) {
            faEntInfoEntity.create();
            faEntInfoMapper.insert(faEntInfoEntity);
        } else {
            faEntInfoMapper.updateById(faEntInfoEntity);
        }
    }


    @Override
    public int delete(String id) {
        return faEntInfoMapper.deleteById(id);
    }


    @Override
    public int remove(String id) {
        return faEntInfoMapper.remove(id);
    }


    public LambdaQueryWrapperX<FaEntInfoEntity> query(FaEntInfoVo faEntInfoVo) {

        LambdaQueryWrapperX<FaEntInfoEntity> faEntInfoEntityLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        faEntInfoEntityLambdaQueryWrapperX = faEntInfoEntityLambdaQueryWrapperX
                .eqIfPresent(FaEntInfoEntity::getId, faEntInfoVo.getId())
                .eqIfPresent(FaEntInfoEntity::getSocialCreditCode, faEntInfoVo.getSocialCreditCode())
                .likeIfPresent(FaEntInfoEntity::getOrgName, faEntInfoVo.getOrgName())
                .eqIfPresent(FaEntInfoEntity::getLinkMan, faEntInfoVo.getLinkMan())
                .eqIfPresent(FaEntInfoEntity::getLinkManPhone, faEntInfoVo.getLinkManPhone())
                .eqIfPresent(FaEntInfoEntity::getLinkManEmail, faEntInfoVo.getLinkManEmail())
                .eqIfPresent(FaEntInfoEntity::getOfficeAddress, faEntInfoVo.getOfficeAddress())
                .eqIfPresent(FaEntInfoEntity::getIndustryId, faEntInfoVo.getIndustryId())
                .eqIfPresent(FaEntInfoEntity::getCreateTime, faEntInfoVo.getCreateTime())
                .eqIfPresent(FaEntInfoEntity::getCreateUser, faEntInfoVo.getCreateUser())
                .eqIfPresent(FaEntInfoEntity::getEnableMark, faEntInfoVo.getEnableMark())

        ;
        return faEntInfoEntityLambdaQueryWrapperX;
    }


}
